package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

import org.xhnews.bo.NewsBo;
import org.xhnews.preferences.Preferences;
import org.xhnews.to.NewsItem;
import org.xhnews.utils.StringUtil;

public class VideoListField extends ListField implements ListFieldCallback,ShowWindow {
	private Vector _videolist;
	Bitmap readPic = Bitmap.getBitmapResource("read.png");
	Bitmap favorPic = Bitmap.getBitmapResource("favor.png");
	Bitmap focusPic = Bitmap.getBitmapResource("focus.png");
	public VideoListField() {
		_videolist = new Vector();
		setRowHeight(getFont().getHeight()*2);
		setCallback(this);
		initialize();
	}

	public void initialize() {		
		reload();
	}

	public void reload() {
		_videolist = NewsBo.getOnlineVideoItems();
		setSize(_videolist.size());
	}

	public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
		NewsItem temp= (NewsItem) _videolist.elementAt(index);
		String title = temp.getTitle();
		String pubDate = temp.getPubDate();
		String source = temp.getChannelTitle();
		if (!temp.isRead())
			g.drawBitmap(0, y + 2, 3, list.getRowHeight() - 2, readPic, 0, 0);

		g.drawText(title, 4, y, 0, w);

		if (temp.isFavorite())
			g.drawBitmap(w - favorPic.getWidth(), y, favorPic.getWidth(),
					favorPic.getHeight(), favorPic, 0, 0);
        g.setColor(Color.GREEN);
		g.drawText(source, 4, y + g.getFont().getHeight() + 2);
		g.setColor(Color.GRAY);
		g.drawText(pubDate + " ", 4, y + g.getFont().getHeight() + 2,
				DrawStyle.RIGHT, w);
		g.drawLine(4, y + g.getFont().getHeight()*2-1, getWidth(), y + g.getFont().getHeight()*2-1);
		g.setColor(Color.BLACK);
	}

	public Object get(ListField list, int index) {
		return _videolist.elementAt(index);
	}

	public int indexOfList(ListField list, String prefix, int string) {
		return _videolist.indexOf(prefix, string);
	}

	public int getPreferredWidth(ListField list) {
		return Display.getWidth();
	}

	private void navigationToVideoPlayerScreen() {
		NewsItem videoitem = (NewsItem) _videolist
				.elementAt(getSelectedIndex());
		String[] subAttach = StringUtil.split(videoitem.getAttach(), ';');
		String filename = "";
		int start = subAttach[0].lastIndexOf('/');
		int end = subAttach[0].lastIndexOf(':');
		filename = subAttach[0].substring(1, end);
		String url = Preferences.server_address + filename;
		VideoPlayerScreen screen = new VideoPlayerScreen(url);
		UiApplication.getUiApplication().pushScreen(screen);
	}

	protected boolean navigationClick(int status, int time) {
		navigationToVideoPlayerScreen();
		return true;
	}

	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			navigationToVideoPlayerScreen();
		}
		return true;
	}
}
